package com.yifeng.repo.endpoint.security.console.proxy;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.talkyun.utils.http.HttpResult;
import com.yifeng.repo.base.utils.converter.JacksonHelper;
import com.yifeng.repo.endpoint.security.console.api.BusinessService;
import com.yifeng.repo.endpoint.security.console.api.dto.AccountPermissionDto;

import java.util.Collections;
import java.util.List;

/**
 * Created by daibing on 2023/8/24.
 */
public class BusinessServiceProxy extends CommonProxy implements BusinessService {
    private final String serverUrl;

    public BusinessServiceProxy(String url, String accessToken, int clientTimeoutMillis) {
        super(url, accessToken, clientTimeoutMillis);
        this.serverUrl = "TODO...";
    }

    @Override
    public List<String> listPublicKey() {
        String url = serverUrl + "/" + "listPublicKey";
        HttpResult result = this.invoke(url, null, clientTimeoutMillis, accessToken);
        return result == null ? Collections.emptyList() : JacksonHelper.toObjList(result.getResult(), String.class);
    }

    @Override
    public AccountPermissionDto getPermission(String code) {
        String url = serverUrl + "/" + "getPermission";
        ObjectNode params = JacksonHelper.createEmptyJsonNode();
        params.put("code", code);
        HttpResult result = this.invoke(url, params.toString(), clientTimeoutMillis, accessToken);
        return result == null ? null : JacksonHelper.toObj(result.getResult(), AccountPermissionDto.class);
    }
}
